import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
      routes: [
        {
          path: '/',
          name: 'Manager',
          component: () => import('@/views/Manager.vue'),
          redirect: '/home',
          children: [
            { path: 'home', name: 'Home', component: () => import('@/views/manager/Home.vue')},
            { path: 'admin', name: 'Admin', component: () => import('@/views/manager/Admin.vue')},
            { path: 'museum', name: 'Museum', component: () => import('@/views/manager/Museum.vue')},
            { path: 'user', name: 'User', component: () => import('@/views/manager/User.vue')},
            { path: 'person', name: 'Person', component: () => import('@/views/manager/Person.vue')},
            { path:'/403', name:'Auth', component: () => import('@/views/manager/Auth.vue'),},
            { path:'/password', name:'Password', component: () => import('@/views/manager/Password.vue'),},
            { path:'/collection', name:'Collection', component: () => import('@/views/manager/Collection.vue'),},
            { path:'/activity', name:'Activity', component: () => import('@/views/manager/Activity.vue'),},
            { path:'/goods', name:'Goods', component: () => import('@/views/manager/Goods.vue'),},
            { path:'/head', name:'Head', component: () => import('@/views/manager/Head.vue'),},
            { path:'userCollection', name:'UserCollection', component: () => import('@/views/manager/UserCollection.vue'),},
            { path:'userGoods', name:'UserGoods', component: () => import('@/views/manager/UserGoods.vue'),},
            { path:'shoppingCar', name:'ShoppingCar', component: () => import('@/views/manager/ShoppingCar.vue'),},
            { path:'userActivity', name:'UserActivity', component: () => import('@/views/manager/UserActivity.vue'),},
            { path:'job', name:'Job', component: () => import('@/views/manager/Job.vue'),},
            { path:'writeOff/:phone', name:'WriteOff', component: () => import('@/views/manager/WriteOff.vue'),},
            { path:'selectWriteOff', name:'SelectWriteOff', component: () => import('@/views/manager/SelectWriteOff.vue'),},


            { path:'order', name:'Order', component: () => import('@/views/manager/Order.vue'),},
            { path:'userOrder', name:'UserOrder', component: () => import('@/views/manager/UserOrder.vue'),},



          ]
        },
        {
          path:'/login',
          name:'Login',
          component: () => import('@/views/Login.vue'),
        },
        {
          path:'/register',
          name:'Register',
          component: () => import('@/views/Register.vue'),
        },
        {
          path:'/test',
          name:'Test',
          component: () => import('@/views/Test.vue'),
        },
        {
          path: '/:pathMatch(.*)*',
          name:'404',
          component: () => import('@/views/404.vue'),
        },
  ]
})
router.beforeEach((to, from, next) => {
  //to 表示要进入的目标路由对象
  // from 表示当前导航正要离开的路由
  // next 是一个函数，表示放行
  let adminPaths = ['/user'];
  let user = JSON.parse(localStorage.getItem("honey-user") || '{}')
  if (user.role !== '管理员' && adminPaths.includes(to.path)) {
    next('/403');
  }else {
    next();
  }
})

export default router
